﻿using Authority.DataTransferObject.MenuAuthority;
using Authority.IServices;
using Authority.IServices.Model.Menu;
using Authority.PresentationModels.Menu;
using AutoMapper;
using HelperFromTheSkeleton.APICore;
using HelperFromTheSkeleton.Model.RequestModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Authority.WebAPI.Controllers
{
    /// <summary>
    /// 菜单权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuAuthorityController : BaseController
    {

        private readonly IMenuAuthorityService  _menuAuthorityService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="MenuAuthorityService"></param>
        /// <param name="mapper"></param>
        public MenuAuthorityController(IMenuAuthorityService  MenuAuthorityService, IMapper mapper)
        {
            _menuAuthorityService = MenuAuthorityService;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Add(AddMenuRequestModel requestModel)
        {
            return await HandleAsync(async() =>
            {
                var model = _mapper.Map<AddMenuModel>(requestModel);
                await _menuAuthorityService.AddAsync(model);
            });
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Edit(EditMenuRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<EditMenuModel>(requestModel);
                await _menuAuthorityService.EditAsync(model);
            });
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel> Delete([Required]Guid id)
        {
            return await HandleAsync(async () =>
            {
                await _menuAuthorityService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取菜单详情
        /// </summary>
        /// <param name="ids">唯一标识集</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<MenuAuthorityDTO>>> Get([Required] List<Guid> ids)
        {
            return await HandleAsync(async () =>
            {
                List<MenuAuthorityDTO> result = await _menuAuthorityService.GetAsync(ids);
                return result;
            });
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<MenuAuthorityListDTO>>> GetOneLevel(Guid? parnetID)
        {
            return await HandleAsync(async () =>
            {
                List<MenuAuthorityListDTO> result = await _menuAuthorityService.GetOneLevelAsync(parnetID);
                return result;
            });
        }

        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<MenuTreeDTO>>> GetTree(Guid SubsystemID)
        {
            return await HandleAsync(async () =>
            {
                List<MenuTreeDTO> result = await _menuAuthorityService.GetTreeAsync(SubsystemID);
                return result;
            });
        }

    }
}
